{%extends "admin/base.html" %}

{%block breadcrumb%}
<ol class="breadcrumb">
    <li class="breadcrumb-item"><a href="/admin">首页</a></li>
    <li class="breadcrumb-item active">系统权限组管理</li>
</ol>
{%endblock%}

{%block tool%}
<a href="{{ url('edit') }}" class="btn btn-sm btn-outline-success float-sm-right mr-3">添加权限组</a>
{%endblock%}

{%block main%}
{% include 'admin/system/auth/index_search.html' %}

<div class="row mt-2">
    <div class="col-12">
        <div class="card">
            <div class="card-body">
                <div class="table-responsive">
                    <table class="table table-head-fixed table-hover text-nowrap table-bordered">
                        <thead>
                            <tr>
                                <th class="text-center">ID</th>
                                <th>名称</th>
                                <th>描述</th>
                                <th class="text-center">状态</th>
                                <th class="text-center">节点数量</th>
                                <th class="text-center">用户数量</th>
                                <th>创建时间</th>
                                <th></th>
                            </tr>
                        </thead>
                        <tbody>
                            {%for v in list.data%}
                            <tr>
                                <td>{{v.id}}</td>
                                <td>{{v.title}}</td>
                                <td>{{v.describe}}</td>
                                <td class="text-center"><input type="checkbox" name="status" data-id="{{v.id}}" {%if v.status==1%} checked{%endif%} data-bootstrap-switch></td>
                                <td class="text-center">{{v.node}}</td>
                                <td class="text-center">{{v.user}}</td>
                                <td>{{v.created_at}}</td>
                                <td>
                                    <a href="{{ url('edit', {'id': v.id}) }}" class="btn btn-sm btn-outline-primary mr-3"><i class="fas fa-edit"></i> 编辑</a>
                                    <a href="{{ url('node', {'id': v.id}) }}" class="btn btn-sm btn-outline-warning mr-3"><i class="fas fa-shield-alt"></i> 授权</a>
                                    <a href="javascript: void(0);" class="btn btn-sm btn-outline-danger btn-delete" data-id="{{v.id}}"><i class="fas fa-trash-alt"></i> 删除</a>
                                </td>
                            </tr>
                            {%endfor%}
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>

<div class="row">
    <div class="col-12">
        {{list.link|raw}}
    </div>
</div>
{%endblock%}

{%block js%}
<script>
    $(function () {
        $("input[data-bootstrap-switch]").each(function () {
            $(this).bootstrapSwitch('state', $(this).prop('checked'));
        });

        $('table input[name="status"]').on('switchChange.bootstrapSwitch', function (event, state) {
            _id = $(this).data('id');
            if (_id <= 0) {
                return;
            }

            $.post('{{ url("state") }}', { id: _id, action: 'status', status: state }, function (res) {
                _msg = ('msg' in res) ? res.msg : '未知错误';
                if (!('code' in res) || res.code != 0) {
                    toastr.error(_msg);
                } else {
                    toastr.success('状态更新成功，稍后重载页面');

                    setTimeout(() => {
                        window.location.reload();
                    }, 1000);
                }
            });
        });

        $('table .btn-delete').on('click', function () {
            _id = $(this).data('id');
            if (_id <= 0) {
                return;
            }

            _confirmStr = '确定要删除该权限组么？<br/><br/>';
            _confirmStr += '<button type="button" class="btn btn-warning clear delete-btn" data-id="' + _id + '">确定</button>';
            toastr.info(_confirmStr, '', {
                closeButton: true, timeOut: 6000, extendedTimeOut: 1000, tapToDismiss: false,
                positionClass: "toast-top-center"
            });
        });

        $('body').on('click', '.delete-btn', function () {
            _id = $(this).data('id');
            if (_id <= 0) {
                return;
            }

            $('#toast-container').remove();

            $.post('{{ url("state") }}', { id: _id, action: 'delete' }, function (res) {
                _msg = ('msg' in res) ? res.msg : '未知错误';
                if (!('code' in res) || res.code != 0) {
                    toastr.error(_msg);
                } else {
                    toastr.success('已删除，稍后重载页面');

                    setTimeout(() => {
                        window.location.reload();
                    }, 1000);
                }
            });
        });
    });
</script>
{%endblock%}